import { toast, getBusinessInfo } from 'nc-lightapp-front';
import { PRIMARY_KEY, LIST_BUTTON, FIELD, STATUS } from '../../constant';
import { listEdit, listBodyDelete, listCopy, listCommit, listUnCommit, blueAddPz, redAddPz, notice, noticeQR } from './listOperator';

//列表操作列按钮操作
export function bodyButtonClick(props, key, text, record, index) {
    let pk = record[PRIMARY_KEY.head_id] && record[PRIMARY_KEY.head_id].value;
    let ts = record[FIELD.ts] && record[FIELD.ts].value;
    let pkMapTs = new Map();
    //主键与tsMap
    if (pk && ts) {
        pkMapTs.set(pk, ts);
    }

    switch (key) {
        //制单
        case LIST_BUTTON.blueAddPzBtn:
            blueAddPz(props, record);
            break;
        //红冲
        case LIST_BUTTON.redAddPzBtn:
            redAddPz(props, record);
            break;
        //通知
        case LIST_BUTTON.noticeBtn:
            notice(props, record);
            break;
        //确认
        case LIST_BUTTON.noticeQrBtn:
            noticeQR(props, record);
            break;

        //修改
        case LIST_BUTTON.bodyUpdate:
            listEdit(props, pk)
            break;

        //删除
        case LIST_BUTTON.bodyDelete:
            listBodyDelete({ ...props, json: this.state.json }, { pks: [pk], pkMapTs }, index);
            break;

        //复制
        case LIST_BUTTON.copy:
            let loginuser = getBusinessInfo().userId;
            let creator = record['creator'] && record['creator'].value;

            if(loginuser != creator){
                toast({ color: STATUS.danger, content: '只允许复制制单人为自己的单据!' });
                return;
            }

            listCopy(props, pk);
            break;

        //提交
        case LIST_BUTTON.bodyCommit:
            listCommit.call(this, { ...props, json: this.state.json }, { pks: [pk], pkMapTs });
            break;

        //收回
        case LIST_BUTTON.bodyUnCommit:
            listUnCommit({ ...props, json: this.state.json }, { pks: [pk], pkMapTs });
            break;
    }
}
